﻿using System;
using System.Diagnostics;
using System.Windows.Forms;

namespace hkgoldenhighlight
{
	internal sealed class Program
	{
		[STAThread]
		private static void Main(string[] args)
		{
			Application.EnableVisualStyles();
			Application.SetCompatibleTextRenderingDefault(false);
			
			if (args.Length > 0)
			{
				
				string firstArgument = args[0].ToLower().Trim();
		        string secondArgument = null;
				
				// Handle cases where arguments are separated by colon. 
				// Examples: /c:1234567 or /P:1234567
				if (firstArgument.Length > 2)
				{
					secondArgument = firstArgument.Substring(3).Trim();
					firstArgument = firstArgument.Substring(0, 2);
				}
				else if (args.Length > 1)
				{
					secondArgument = args[1];
				}
		
				/*
				if(firstArgument == "/deleteupdatefiles") //App Update: Delete unused files
				{
					#region delete files
					string updaterPath = System.IO.Path.Combine(Application.StartupPath, "hkgoldenhighlight-updater.exe");
					string oldScrPath = System.IO.Path.Combine(Application.StartupPath, "hkgoldenhighlight-old.scr");
					
					if(System.IO.File.Exists(updaterPath)){
						System.IO.File.Delete(updaterPath);
					}
					
					if(System.IO.File.Exists(oldScrPath)){
					   	System.IO.File.Delete(oldScrPath);
					}
					#endregion
					MessageBox.Show(args[0], "test");
					Application.Run(new SettingsForm());
				}
				else */
				if (firstArgument == "/c")           // Configuration mode
				{
					Application.Run(new SettingsForm());
				}
				else if (firstArgument == "/p")      // Preview mode
				{
					if (secondArgument == null)
					{
						MessageBox.Show("Sorry, but the expected window handle was not provided.",
							"ScreenSaver", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
						return;
					}
				
					IntPtr previewWndHandle = new IntPtr(long.Parse(secondArgument));
					Application.Run(new ScreenSaverForm(previewWndHandle));
				}
				else if (firstArgument == "/s")      // Full-screen mode
				{
					ShowScreenSaver();
					Application.Run();
				}
				else    // Undefined argument 
				{
					MessageBox.Show("參數及值「" + firstArgument + "」並不合法。", Application.ProductName , MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				}
			}
			else    // No arguments - treat like /c
			{
				Application.Run(new SettingsForm());
			}            
		}

		private static void ShowScreenSaver()
		{
			// old seafood
			/*
			foreach (Screen screen in Screen.AllScreens)
			{
				ScreenSaverForm screensaver = new ScreenSaverForm(screen.Bounds);
				screensaver.Show();
			}
			*/
			ScreenSaverForm screensaver = new ScreenSaverForm();
			screensaver.Show();
		}
	}
}
